package com.ruoyi.bantong.service.impl;

import java.util.List;

import com.ruoyi.bantong.domain.BtArticleView;
import com.ruoyi.bantong.mapper.BtArticleViewMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.mapper.BtArticleMapper;
import com.ruoyi.bantong.domain.BtArticle;
import com.ruoyi.bantong.service.IBtArticleService;

/**
 * 文章Service业务层处理
 * 
 * @author laohei
 * @date 2021-01-24
 */
@Service
public class BtArticleServiceImpl implements IBtArticleService 
{
    @Autowired
    private BtArticleMapper btArticleMapper;


    @Autowired
    private BtArticleViewMapper viewMapper;

    /**
     * 查询文章
     * 
     * @param artId 文章ID
     * @return 文章
     */
    @Override
    public BtArticle selectBtArticleById(Integer artId)
    {
        return btArticleMapper.selectBtArticleById(artId);
    }


    /**
     * 增加播放量
     *
     * @param artId 文章ID
     * @return 文章
     */
    @Override
    public Integer addArticleView(Integer artId) {
        Integer btUserId = SecurityUtils.getBtUserId();
        //增加播放量
        if(!StringUtils.isNull(btUserId)){
            BtArticleView btArticleView = viewMapper.selectBtArticleViewById(artId, btUserId);
            if(StringUtils.isNull(btArticleView)){
                BtArticle btArticle = new BtArticle();
                btArticle.setArtId(artId);
                btArticle.setView(1);
                btArticle.setUpdateTime(DateUtils.getNowDate());
                btArticleMapper.updateBtArticle(btArticle);
                btArticleView = new BtArticleView();
                btArticleView.setArtId(artId);
                btArticleView.setUserId(btUserId);
                btArticleView.setCreateTime(DateUtils.getNowDate());
                viewMapper.insertBtArticleView(btArticleView);
            }
        }
        return 1;
    }

    /**
     * 查询文章列表
     * 
     * @param btArticle 文章
     * @return 文章
     */
    @Override
    public List<BtArticle> selectBtArticleList(BtArticle btArticle)
    {
        return btArticleMapper.selectBtArticleList(btArticle);
    }

    /**
     * 新增文章
     * 
     * @param btArticle 文章
     * @return 结果
     */
    @Override
    public int insertBtArticle(BtArticle btArticle)
    {
        btArticle.setCreateTime(DateUtils.getNowDate());
        return btArticleMapper.insertBtArticle(btArticle);
    }

    /**
     * 修改文章
     * 
     * @param btArticle 文章
     * @return 结果
     */
    @Override
    public int updateBtArticle(BtArticle btArticle)
    {
        btArticle.setUpdateTime(DateUtils.getNowDate());
        return btArticleMapper.updateBtArticle(btArticle);
    }

    /**
     * 批量删除文章
     * 
     * @param artIds 需要删除的文章ID
     * @return 结果
     */
    @Override
    public int deleteBtArticleByIds(Integer[] artIds)
    {
        return btArticleMapper.deleteBtArticleByIds(artIds);
    }

    /**
     * 删除文章信息
     * 
     * @param artId 文章ID
     * @return 结果
     */
    @Override
    public int deleteBtArticleById(Integer artId)
    {
        return btArticleMapper.deleteBtArticleById(artId);
    }
}
